#!/usr/bin/python
"""

check_files_for_valid_end_markers.py

	--file_names_wildcard "*.blast_res"
    --file_end_marker "#Blastall EOF"

    checks results of parse_blastall_output.pl
    rename errant files to "*.interrupted_errors"

"""

from optparse import OptionParser
import sys, os
import os.path
import re
import StringIO
import glob
import shutil

# add self to search path for testing
if __name__ == '__main__':
    exe_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
    sys.path.append(os.path.abspath(os.path.join(exe_path,"..", "python_modules")))
    myname = os.path.split(sys.argv[0])[1]
    myname = os.path.splitext(myname)[0];
else:
    myname = __name__

import general_util
from custom_log import custom_log
from general_util import die_error




parser = OptionParser(version="%prog 1.0")
parser.add_option("-f", "--file_names_wildcard", dest="file_names_wildcard",
                  metavar="FILES", 
                  type="string",
                  help="name and path of log file")
parser.add_option("-L", "--log_file_name", dest="logfile",
                  metavar="FILE", 
                  type="string",
                  help="name and path of log file")
parser.add_option("-e", "--file_end_marker", dest="file_end_marker",
                  metavar="STRING", 
                  type="string",
                  help="marker used to indicate end of valid files.")
parser.add_option("-q", "--quiet", dest = "quiet",
                  action="store_true", default=False,
                  help="Do not echo to shell but only print to log.")
parser.set_defaults(logfile = "") 
parser.set_defaults(file_end_marker = "") 
parser.set_defaults(file_names_wildcard = "") 

# get help string
f =StringIO.StringIO()
parser.print_help(f)
helpstr = f.getvalue()
(options, args) = parser.parse_args()

# mandatory options
if options.file_names_wildcard == "":
    die_error("Please specify which files to check in --file_names_wildcard.\n\n"+helpstr)
if options.file_end_marker == "":
    die_error("Please specify the marker (via --file_end_marker) used to " +
              "indicate end of valid files.\n\n" + helpstr)

status_log = custom_log(myname, options.logfile, options.quiet)
status_log.no_new_line_on_exit()


import glob
files = glob.glob(options.file_names_wildcard)
files.sort()
count_corrupted_files = 0
for file_name in files:
    # if not null file read end for marker
    if os.path.getsize(file_name):
        file = open(file_name, 'rb')
        file.seek(-len(options.file_end_marker) - 1, 2)
        text = file.read(50)

        # corrupted file
        #if "#Blastall EOF" != text[:-1]:
        if options.file_end_marker != text[:-1]:
            file.close()
            os.rename(file_name, file_name + ".interrupted_errors")
            count_corrupted_files += 1
        
if count_corrupted_files:
    status_log.warning(str(count_corrupted_files) + " corrupted files")
    sys.exit(255)

# print out non-comment lines
count_non_empty_files = 0
for file_name in files:
    # if not null file read end for marker
    if os.path.getsize(file_name):
        count_non_empty_files += 1
        for line in open(file_name):
            if line[0] <> '#':
                sys.stdout.write(line)

    
status_log.info(str(len(files)) + " valid files read")
status_log.info(str(count_non_empty_files) + " non-empty files written")

